﻿using FollowMe.Services;
using FollowMe.Services.Request;
using FollowMe.Services.Response;
using Microsoft.Practices.Unity;
using System;
using System.Web.Http;

/**
 * FollowMe Server
 * UWRF CS425 & CS343 Project Fall 2014
 * Adam Drews
 *   email adam.drews@my.uwrf.edu
 * Tyler Johnson
 *   email tyler.johnson@my.uwrf.edu
 * Nicholas Thorp
 *   email nicholas.thorp@my.uwrf.edu
 */
namespace FollowMe.API.Controllers
{

    /// <summary>
    /// Messaging related API functions
    /// </summary>
    [RoutePrefix("api/Message")]
    public class MessageController : ApiController
    {
        /// <summary>
        /// The  follow me services
        /// </summary>
        private IFollowMeService _services;

        /// <summary>
        /// Initializes a new instance of the <see cref="MessageController"/> class.
        /// </summary>
        public MessageController()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MessageController"/> class.
        /// </summary>
        /// <param name="services">The follow me services.</param>
        [InjectionConstructor]
        public MessageController(FollowMeService services)
        {
            _services = services;
        }

        
         //POST api/Message/Send
        /// <summary>
        /// Sends the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>The sent message on success</returns>
        [Route("Send", Name = "Send")]
        [System.Web.Http.AcceptVerbs("POST")]
        [System.Web.Http.HttpPost]
        [Authorize]
        public IHttpActionResult Send(SendMessageRequest message)
        {
            MessageView mess = _services.sendMessageTo(message);
            if (mess == null)
                return BadRequest("Memeber must exist, and you must be friends!");

            return Ok(mess);
        }

        //GET api/Message/GetSent
        /// <summary>
        /// Gets the sent.
        /// </summary>
        /// <param name="memberId">The member id of the user requesting.</param>
        /// <returns>All messages sent as MessageViews</returns>
        [Route("GetSent", Name = "GetSent")]
        [System.Web.Http.AcceptVerbs("GET")]
        [System.Web.Http.HttpGet]
        [AllowAnonymous]
        public IHttpActionResult GetSent(string memberId)
        {
            if (memberId == null) { return BadRequest("null"); }
            Guid memberGuid;
            memberId = memberId.Replace("(", "").Replace("{", "").Replace("}", "").Replace(")", "").Replace("-", "");
            if (!Guid.TryParseExact(memberId, "N", out memberGuid))
            {
                return BadRequest("Bad GUID");
            }
            FindMemberInfoRequest member = new FindMemberInfoRequest
            {
                memberid = memberGuid
            };

            return Ok(_services.findSentMessagesFrom(member));
        }


        //GET api/Message/GetReceived
        /// <summary>
        /// Gets the messages sent to a user.
        /// </summary>
        /// <param name="memberId">The member id of the user searching.</param>
        /// <returns>All messages received as MessageViews</returns>
        [Route("GetReceived", Name = "GetReceived")]
        [System.Web.Http.AcceptVerbs("GET")]
        [System.Web.Http.HttpGet]
        [AllowAnonymous]
        public IHttpActionResult GetReceived(string memberId)
        {
            if (memberId == null) { return BadRequest("null"); }
            Guid memberGuid;
            memberId = memberId.Replace("(", "").Replace("{", "").Replace("}", "").Replace(")", "").Replace("-", "");
            if (!Guid.TryParseExact(memberId, "N", out memberGuid))
            {
                return BadRequest("Bad GUID");
            }
            FindMemberInfoRequest member = new FindMemberInfoRequest
            {
                memberid = memberGuid
            };

            return Ok(_services.findReceivedMessagesFor(member));
        }

        // api/Message/Unread
        /// <summary>
        /// Gets the unread.
        /// </summary>
        /// <param name="memberId">The member id of the user requesting.</param>
        /// <returns>All unread messages received as MessageViews</returns>
        [Route("GetUnread", Name = "GetUnread")]
        [System.Web.Http.AcceptVerbs("GET")]
        [System.Web.Http.HttpGet]
        [AllowAnonymous]
        public IHttpActionResult GetUnread(string memberId)
        {
            if (memberId == null) { return BadRequest("null"); }
            Guid memberGuid;
            memberId = memberId.Replace("(", "").Replace("{", "").Replace("}", "").Replace(")", "").Replace("-", "");
            if (!Guid.TryParseExact(memberId, "N", out memberGuid))
            {
                return BadRequest("Bad GUID");
            }
            FindMemberInfoRequest member = new FindMemberInfoRequest
            {
                memberid = memberGuid
            };

            return Ok(_services.findUnreadMessagesFor(member));
        }

        //PUT api/Message/MarkRead
        /// <summary>
        /// Marks a message as read.
        /// </summary>
        /// <param name="messageId">The message id of the message.</param>
        /// <returns>The results of the put</returns>
        [Route("MarkRead", Name = "MarkRead")]
        [System.Web.Http.AcceptVerbs("PUT")]
        [System.Web.Http.HttpPut]
        [AllowAnonymous]
        public IHttpActionResult MarkRead(MessageRequest messageId)
        {
            if (messageId == null) { return BadRequest("null"); }

            return Ok(_services.markAsRead(messageId));
        }
    }
}